﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Threading;
using System.ComponentModel;
using System.Windows.Threading;

namespace AlgoLogicMessengerCompetitor
{
    /// <summary>
    /// Interaction logic for NavigationPage.xaml
    /// </summary>
    public partial class NavigationPage : Page
    {
        //sockets
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
        BackgroundWorker worker = new BackgroundWorker();
        Thread ctThread1;
        public NavigationPage()
        {
            InitializeComponent();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            clientSocket.Connect(IP.Address, 8888);
            serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("" + "#");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += delegate(object s, DoWorkEventArgs args)
                            {          
                                getCommand();
                            };
            worker.RunWorkerAsync();
           // BtnBQuiz.IsEnabled = false;
        }
        
        private void getCommand()
        {
            while (true)
            {
                try
                {
                    serverStream = clientSocket.GetStream();               
                    int bufferSize = clientSocket.ReceiveBufferSize;
                    byte[] inStream = new byte[bufferSize];   
                    serverStream.Read(inStream, 0, bufferSize);

                    string str = System.Text.Encoding.ASCII.GetString(inStream, 0, inStream.Length);
                    str = str.Replace("\0", "");
                    string[] array1 = str.Split('|');

                    if (str.StartsWith("@quizStart"))
                    {
                        Thread.Sleep(100);
                        this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            NavigationFrame.Content = null;
                            NavigationFrame.NavigationService.RemoveBackEntry();
                            QuestionAnswer Qa = new QuestionAnswer();
                            NavigationFrame.Content = Qa;
                        }
                          )
                          );
                    }
                    else if (str.StartsWith("@AllowNavigation"))
                    {
                        this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            //BtnBQuiz.IsEnabled = false;
                            //BtnGames.IsEnabled = true;
                            //tictactoeBtn.IsEnabled = true;
                            //reversiBtn.IsEnabled = true;
                        }
                          )
                          );
                    }
                    else if (str.StartsWith("@DisallowNavigation"))
                    {
                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            //BtnBQuiz.IsEnabled = false;
                            //BtnGames.IsEnabled = false;
                            tictactoeBtn.IsEnabled = false;
                            reversiBtn.IsEnabled = false;
                        }
                          )
                          );
                    }
                    else if (str.StartsWith("@ForceAllLogOut"))
                    {
                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            Environment.Exit(0);
                        }
                          )
                          );
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        ////private void button1_Click(object sender, RoutedEventArgs e)
        //{
        //    NavigationService nav = NavigationService.GetNavigationService(this);
        //    nav.Navigate(new System.Uri("QuestionAnswer.xaml", UriKind.RelativeOrAbsolute));
        //}

        //private void button2_Click(object sender, RoutedEventArgs e)
        //{
        //    NavigationService nav = NavigationService.GetNavigationService(this);
        //    nav.Navigate(new System.Uri("Games.xaml", UriKind.RelativeOrAbsolute));
        //}

        private void Page_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            string text = e.Key.ToString();
            if (e.Key == Key.Back)
            {
                if (text != "")
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            else if (e.Key == Key.F5)
            {
                e.Handled = true;
            }
        }

        private void tictactoeBtn_Click(object sender, RoutedEventArgs e)
        {
            NavigationFrame.Content = null;
            NavigationFrame.NavigationService.RemoveBackEntry();
            GameWindow Qa = new GameWindow();
            NavigationFrame.Content = Qa;
        }

        private void reversiBtn_Click(object sender, RoutedEventArgs e)
        {
            NavigationFrame.Content = null;
            NavigationFrame.NavigationService.RemoveBackEntry();
            Reversi Qa = new Reversi();
            NavigationFrame.Content = Qa;
        }
    }
}